package petertel.pbx.conf;

import java.util.ArrayList;
import java.util.List;

public abstract class ConfigItem<CT extends ConfigItem<?>> {

	private int lineno;
	private Flag flag;

	protected List<CT> children = new ArrayList<CT>();

	public void addChild(int index,CT child) {
		if(index==-1)
			children.add(child);
		else
			children.add(index,child);
	}

	public List<CT> getChildren() {
		List<CT> ret = new ArrayList<CT>();
		for (CT child : children) {
			if (child.getFlag() == Flag.delete) {
				continue;
			}
			ret.add(child);
		}
		return ret;
	}
	
	public void removeChildren(int index){
		getChildren().get(index).setFlag(Flag.delete);
	}

	protected int relocateIndex(int index) {
		for (int i = 0; i <= index; i++) {
			CT child = children.get(i);
			if (child.getFlag() == Flag.delete) {
				index++;
			}
		}
		return index;
	}

	public int getLineno() {
		return lineno;
	}

	public void setLineno(int lineno) {
		this.lineno = lineno;
	}

	public Flag getFlag() {
		return flag;
	}

	public void setFlag(Flag flag) {
		this.flag = flag;
	}

	public static enum Flag {
		insert, delete, update
	}

	public abstract String write();
}
